Trò chơi Pac-Man

20.576 lượt xem;
1 using System;
2 using
System.Collections;
3 using
System.Collections.Generic;
4 using
System.IO;
5 using
UnityEngine;
6
7 public
class ScoreManager : MonoBehaviour {
8
9     
private string TopScoresURL = "http://ilbeyli.byethost18.com/pacman/topscores.php";
10     
private string username;
11     
private int _highscore;
12     
private int _lowestHigh;
13     
private bool _scoresRead;
14     
private bool _isTableFound;
15
16     
public class Score
17     {
18         
public string name { get; set; }
19         
public int score { get; set; }
20
21         
public Score(string n, int s)
22         {
23             name = n;
24             score = s;
25         }
26
27         
public Score(string n, string s)
28         {
29             name = n;
30             score = Int32.Parse(s);
31         }
32     }
33
34     List<Score> scoreList =
new List<Score>(10);
35
36     
void OnLevelWasLoaded(int level)
37     {
38         
//StartCoroutine("ReadScoresFromDB");
39
40         
if (level == 2) StartCoroutine("UpdateGUIText"); // if scores is loaded
41         
if (level == 1) _lowestHigh = _highscore = 99999;
42         
//if (level == 1) StartCoroutine("GetHighestScore"); // if game is loaded
43     }
44
45     IEnumerator GetHighestScore()
46     {
47         Debug.Log(
"GETTING HIGHEST SCORE");
48         
// wait until scores are pulled from database
49         
float timeOut = Time.time + 4;
50         
while (!_scoresRead)
51         {
52             
yield return new WaitForSeconds(0.01f);
53             
if (Time.time > timeOut)
54             {
55                 Debug.Log(
"Timed out");
56                 
//scoreList.Clear();
57                 
//scoreList.Add(new Score("GetHighestScore:: DATABASE CONNECTION TIMED OUT", -1));
58                 
break;
59             }
60         }
61
62         _highscore = scoreList[
0].score;
63         _lowestHigh = scoreList[scoreList.Count -
1].score;
64     }
65
66     IEnumerator UpdateGUIText()
67     {
68         
/*
69         // wait until scores are pulled
from database
70         
float timeOut = Time.time + 4;
71         
while (!_scoresRead)
72         {
73             
yield return new WaitForSeconds(0.01f);
74             
if (Time.time > timeOut)
75             {
76                 //Debug.Log(
"TIMEOUT!");
77                 scoreList.Clear();
78                 scoreList.Add(
new Score("DATABASE TEMPORARILY UNAVAILABLE", 999999));
79                 
break;
80             }
81         }
82         */

83         scoreList.Clear();
84         scoreList.Add(
new Score("DATABASE TEMPORARILY UNAVAILABLE", 999999));
85
86         GameObject.FindGameObjectWithTag(
"ScoresText").GetComponent<Scores>().UpdateGUIText(scoreList);
87         
yield return new WaitForSeconds(0f);
88     }
89
90     IEnumerator ReadScoresFromDB()
91     {
92         WWW GetScoresAttempt =
new WWW(TopScoresURL);
93         
yield return GetScoresAttempt;
94
95         
if (GetScoresAttempt.error != null)
96         {
97             Debug.Log(
string.Format("ERROR GETTING SCORES: {0}", GetScoresAttempt.error));
98             scoreList.Add(
new Score(GetScoresAttempt.error, 1234));
99             StartCoroutine(UpdateGUIText());
100         }
101         
else
102         {
103             
// ATTENTION: assumes query will find table
104
105             
string[] textlist = GetScoresAttempt.text.Split(new string[] { "\n", "\t" },
106                 StringSplitOptions.RemoveEmptyEntries);
107
108             
if (textlist.Length == 1)
109             {
110                 
//`Debug.Log("== 1");
111                 scoreList.Clear();
112                 scoreList.Add(
new Score(textlist[0], -123));
113                 
yield return null;
114             }
115             
else
116             {
117
118
119                 
string[] Names = new string[Mathf.FloorToInt(textlist.Length/2)];
120                 
string[] Scores = new string[Names.Length];
121
122                 
//Debug.Log("Textlist length: " + textlist.Length + " DATA: " + textlist[0]);
123                 
for (int i = 0; i < textlist.Length; i++)
124                 {
125                     
if (i%2 == 0)
126                     {
127                         Names[Mathf.FloorToInt(i/
2)] = textlist[i];
128                     }
129                     
else Scores[Mathf.FloorToInt(i/2)] = textlist[i];
130                 }
131
132                 
for (int i = 0; i < Names.Length; i++)
133                 {
134                     scoreList.Add(
new Score(Names[i], Scores[i]));
135                 }
136
137                 _scoresRead =
true;
138             }
139         }
140
141     }
142
143     
public int High()
144     {
145         
return _highscore;
146     }
147
148     
public int LowestHigh()
149     {
150         
return _lowestHigh;
151     }
152 }


private string TopScoresURL = "http:ilbeyli.byethost18.compacmantopscores.php";

StartCoroutine("ReadScoresFromDB");

if (level == 2) StartCoroutine("UpdateGUIText"); if scores is loaded

if (level == 1) StartCoroutine("GetHighestScore"); if game is loaded

wait until scores are pulled from database

scoreList.Clear();

scoreList.Add(new Score("GetHighestScore:: DATABASE CONNECTION TIMED OUT", -1));

wait until scores are pulled from database

Debug.Log("TIMEOUT!");

ATTENTION: assumes query will find table

`Debug.Log("== 1");

Debug.Log("Textlist length: " + textlist.Length + " DATA: " + textlist[0]);



Gõ tìm kiếm nhanh...